﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Passwords.Pages;
using Silvermoon.Windows;
using Silvermoon.Controls;
using Silvermoon.UI;
using Passwords.Models;
using Silvermoon.Controls.Forms;
using Silvermoon.Navigation;
using Passwords.Biz;

namespace Passwords.Controllers
{
    public class PasswordsController : IDisposable
    {
        private static NavigationService navigator = new NavigationService();
        public PasswordsPage PasswordsPage { get; private set; }

        public PasswordsController(PasswordsPage page)
            : base()
        {
            this.PasswordsPage = page;
            PasswordsControllerHelper.PasswordsPage = page;
            Context.Current.PasswordDeleted += OnPasswordDeleted;
            Context.Current.PasswordAdded += new EventHandler<PasswordEventArgs>(OnPasswordInserted);
            Context.Current.PasswordSaved += OnPasswordModified;

            PasswordCommands.NewPassword.Executed += OnNewPassword;

            PasswordsTab tabs = page.TabView;
            AllPasswordsController = PasswordsControllerHelper.InitializeAllPasswordsController(tabs.AllListView);
            CategoryPasswordController = PasswordsControllerHelper.InitializeCategoryPasswordsController(tabs.CategoriesListView);
            FavesPasswordsController = PasswordsControllerHelper.InitializeFavesController(tabs.FavesListView);

        }


        public void Dispose()
        {
            if (Context.Current != null)
            {
                Context.Current.PasswordAdded -= OnPasswordInserted;
                Context.Current.PasswordDeleted -= OnPasswordDeleted;
                Context.Current.PasswordSaved -= OnPasswordModified;
            }
            PasswordCommands.NewPassword.Executed -= OnNewPassword;
        }

        public ListViewController<TitleDetailsImageItem, Password> AllPasswordsController;
        public ListViewController<TitleDetailsImageItem, Password> FavesPasswordsController;
        public ListViewController<TitleDetailsImageItem, CategorizedPassword> CategoryPasswordController;

        void OnFavoriteRemoved(object sender, PasswordEventArgs e)
        {
            int id = e.Password.Id;
            var pw = FavesPasswordsController.DataSource.First(p => p.Id == id);
            if (pw != null) FavesPasswordsController.RemoveValue(pw);
        }

        void OnFavoriteAdded(object sender, PasswordEventArgs e)
        {
            int id = e.Password.Id;
            FavesPasswordsController.DataSource = Context.Current.Favorites;
            var pw = FavesPasswordsController.DataSource.First(p => p.Id == id);
            if (pw != null) FavesPasswordsController.AddValue(pw);
        }

        void OnPasswordModified(object sender, PasswordEventArgs e)
        {
            Password p = e.Password;
            if (p.IsFavoriteModified)
            {
                if (p.IsFavorite) OnFavoriteAdded(sender, e); else OnFavoriteRemoved(sender, e);
            }
        }

        void OnPasswordInserted(object sender, PasswordEventArgs e)
        {
            Password p = e.Password;
            AllPasswordsController.AddValue(p);
            var cps = p.Categories.Select(c => new CategorizedPassword(c, p));
            foreach (var cp in cps)
            {
                CategoryPasswordController.AddValue(cp);
            }
        }

        void OnPasswordDeleted(object sender, PasswordEventArgs e)
        {
            DeletePassword(e.Password);
        }

        private void OnNewPassword(object sender, CommandEventArgs args)
        {
            EditDetailsPage page = new EditDetailsPage();
            page.Password = new Password();
            PasswordsPage.NavigateTo(page);

            //NewPasswordCategoriesPage page = new NewPasswordCategoriesPage();
            ////PasswordsPage.NavigateTo(page, PageTransitionMode.Show);
            //PasswordsPage.NavigateTo(page, PageTransitions.FromBottom);
        }

        private void DeletePassword(Password password)
        {
            try
            {
                foreach(Category c in Context.Current.Categories)
                {
                    var pi = new CategorizedPassword(c, password);
                    CategoryPasswordController.RemoveValue(pi);
                }
                AllPasswordsController.RemoveValue(password);
                FavesPasswordsController.RemoveValue(password);
            }
            catch (Exception e)
            {
                MessageDialog.Show("Database Error", e.Message);
            }
        }
    }
}
